/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.behaviours;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import java.util.HashMap;

/**
 *
 * @author raider
 */
public class OfferRequestsServer extends CyclicBehaviour {

    private HashMap catalogue;

    public OfferRequestsServer(HashMap catalogue) {
        this.catalogue = catalogue;
    }
    
    @Override
    public void action() {
        MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP);
        ACLMessage msg = myAgent.receive(mt);
        if (msg != null) {
// Message received. Process it
            String title = msg.getContent();
            ACLMessage reply = msg.createReply();
            Integer price = (Integer) catalogue.get(title.split(",")[0]);
            if (price != null) {
// The requested book is available for sale. Reply with the price
                reply.setPerformative(ACLMessage.PROPOSE);
                reply.setContent(String.valueOf(price.intValue()));
            } else {
// The requested book is NOT available for sale.
                reply.setPerformative(ACLMessage.REFUSE);
                reply.setContent("not-available");
            }
            myAgent.send(reply);
        }else{
            block();
        }
    }
}